#include "timerbtn.h"
#include <QDebug>
#include <QMouseEvent>

TimerBtn::TimerBtn(QWidget *parent):QPushButton(parent),counter_(60)
{
    timer_ = new QTimer(this);
    connect(timer_, &QTimer::timeout, [this](){
        counter_--;
        if(counter_ <= 0){
            timer_->stop();
            counter_ = 60;
            this->setText("获取");
            this->setEnabled(true);
            return;
        }
        this->setText(QString::number(counter_));
    });

}

void TimerBtn::mouseReleaseEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        this->setEnabled(false);
        this->setText(QString::number(counter_));
        timer_->start(1000);
        emit clicked();
    }
    // 调用基类的mouseReleaseEvent以确保正常的事件处理（如点击效果）
    QPushButton::mouseReleaseEvent(e);
}

TimerBtn::~TimerBtn()
{
    timer_->stop();
}
